package com.space.school.controller;

import com.space.school.domain.Category;
import com.space.school.services.CategoryService;
import com.space.school.services.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "categoryOperateServlet",value = "/categoryOperateServlet")
public class CategoryOperateServlet extends HttpServlet {
   CategoryService categoryService=new CategoryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id=0;
        req.setCharacterEncoding("UTF-8");
        String categoryId = req.getParameter("category_id");
        String operate = req.getParameter("operate");
        if(categoryId!=null){
            id=Integer.parseInt(categoryId);
        }
//       删除操作
        if(operate.equals("del")){
            int num=categoryService.delete(id);
            if(num>0){
//                跳转页面  userServlet doget方法 Redirect 重定向
                resp.sendRedirect("/categoryServlet");
            }

        }else if(operate.equals("update")){
//            先把userid 对应的值取出来
            Category category =categoryService.findById(id);
            if(category !=null){
//                往里面防值
                req.setAttribute("category", category);
                req.getRequestDispatcher("updateCategoryList.jsp").forward(req,resp);
            }

        }else if(operate.equals("add")){
            req.getRequestDispatcher("insertUserList.jsp").forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
